package com.ouc.eddyAlgorithm.controller;

import com.ouc.common.config.properties.ApplicationProperties;
import com.ouc.common.core.domain.AjaxResult;
import com.ouc.common.utils.DateUtils;
import com.ouc.common.utils.SystemUtils;
import com.ouc.common.utils.TextUtils;
import com.ouc.eddyAlgorithm.domain.EddyGed;
import com.ouc.eddyAlgorithm.service.EddyAlgorithmSetService;
import com.ouc.eddyAlgorithm.service.IEddyGedService;
import com.ouc.eddyAlgorithm.utils.PythonPetUtil;
import com.ouc.eddyAlgorithm.utils.PythonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.*;

/**
 * 调用python方法
 *
 * @author B504
 */
@Controller
@CrossOrigin
@RequestMapping(value = "/python")
public class PythonController {

  @Autowired
  ApplicationProperties applicationProperties;

  @Autowired
  EddyAlgorithmSetService eddyAlgorithmSetService;

  @Autowired
  IEddyGedService iEddyGedService;

  /**
   * 使用python的pet方法接口
   *
   * @param pythonName python文件位置
   * @param ncFilePath nc文件位置
   * @param startLon   开始经度
   * @param endLon     结束经度
   * @param startLat   开始纬度
   * @param endLat     结束纬度
   * @return
   */
  @RequestMapping("/getPetByPython")
  @ResponseBody
  public AjaxResult getPetByPython(@RequestParam("pythonPath") String pythonName,
                                   @RequestParam("ncFilePath") String ncFilePath, @RequestParam("startLon") Integer startLon, @RequestParam("endLon") Integer endLon,
                                   @RequestParam("startLat") Integer startLat, @RequestParam("endLat") Integer endLat) {
    try {
      eddyAlgorithmSetService.truncateTable();

      if (SystemUtils.isWindows()) {
        return AjaxResult.success(PythonPetUtil.startPythonOfPet(applicationProperties.getPetPython() + File.separator + pythonName, ncFilePath, startLon, endLon, startLat, endLat));
      } else {
        return AjaxResult.success(PythonPetUtil.startPythonOfPet(applicationProperties.getPetPythonLinux() + File.separator + pythonName, ncFilePath, startLon, endLon, startLat, endLat));
      }
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResult.error(e.getMessage());
    }
  }

  /**
   * 通过cmd调用深度学习接口
   *
   * @param pythonPath python文件
   * @param ncFilePath nc文件
   * @param startLon   开始经度
   * @param endLon     结束经度
   * @param startLat   开始纬度
   * @param endLat     结束纬度
   * @return
   */
  @RequestMapping("/getDeepLearnByPython")
  @ResponseBody
  public AjaxResult getDeepLearnByPython(@RequestParam("pythonPath") String pythonPath,
                                         @RequestParam("ncFilePath") String ncFilePath, @RequestParam("startLon") Integer startLon, @RequestParam("endLon") Integer endLon,
                                         @RequestParam("startLat") Integer startLat, @RequestParam("endLat") Integer endLat) {
    try {
      return AjaxResult.success(PythonPetUtil.startDeepLearning(pythonPath, ncFilePath, startLon, endLon, startLat, endLat));
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResult.error(e.getMessage());
    }
  }

  /**
   * 获取gedyolo算法算出的txt的涡旋信息
   *
   * @param pythonName python文件名
   * @param ncFilePath nc文件名
   * @param startLon   开始经度
   * @param endLon     结束经度
   * @param startLat   开始纬度
   * @param endLat     结束纬度
   * @param confidence 置信度
   * @param nmsIou     非极大抑制
   * @return
   */
  @RequestMapping("/getGedYolo")
  @ResponseBody
  public AjaxResult getGedYolo(@RequestParam("pythonName") String pythonName,
                               @RequestParam("ncFilePath") String ncFilePath,
                               @RequestParam("startLon") String startLon,
                               @RequestParam("endLon") String endLon,
                               @RequestParam("startLat") String startLat,
                               @RequestParam("endLat") String endLat,
                               @RequestParam(value = "confidence", defaultValue = "0.3", required = false) String confidence,
                               @RequestParam(value = "nmsIou", defaultValue = "0.1", required = false) String nmsIou) {
    try {
      iEddyGedService.truncateTable();
      String pythonPath = applicationProperties.getGedyoloDir() + File.separator + pythonName;
      PythonUtil.startGedYolo(pythonPath, ncFilePath, startLon, endLon, startLat, endLat, confidence, nmsIou);
      List<String> listString = TextUtils.readTxt(applicationProperties.getGedyoloDir() + File.separator + "GED-YOLO.txt", 2);
      List<EddyGed> eddyGedList = new ArrayList<>();
      for (String s : listString) {
        EddyGed eddyGed = new EddyGed();
        String[] s1 = s.split(" ");
        eddyGed.setEddyType(Integer.valueOf(s1[1]));
        eddyGed.setEddyScore(Float.valueOf(s1[2]));
        eddyGed.setEddyDate(DateUtils.parseDate(s1[3]));
        eddyGed.setEddyRadius(Float.valueOf(s1[4]));
        eddyGed.setEddyLat(Float.valueOf(s1[5]));
        eddyGed.setEddyLon(Float.valueOf(s1[6]));
        eddyGedList.add(eddyGed);
      }
      iEddyGedService.insertByBach(eddyGedList);
      return AjaxResult.success("成功生成txt并插入数据库");
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResult.error("获取失败");
    }
  }
}
